/// @tags: Construct
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define rint register int

using std::cin;
using std::cout;

namespace BlueQuantum {

constexpr int maxn = 53, inf = 0x3f3f3f3f;

int n;

struct Edge {
  int v, nxt;
} e[maxn * 2];

struct Graph {
  int map[maxn][maxn], p[maxn][2], eid[maxn], deg[maxn];
  int heads[maxn], ecnt;
  int fa[maxn];

  inline void clear() {
    memset(map, 0, sizeof(map));
    memset(p, 0, sizeof(p));
    memset(deg, 0, sizeof(deg));
    memset(eid, 0, sizeof(eid));
    memset(heads, 0, sizeof(heads));
    ecnt = 0;
  }

  Edge e[maxn * 2];

  inline void add(int u, int v) { e[++ecnt].v = v, e[ecnt].nxt = heads[u], heads[u] = ecnt; }

  inline void link(int u, int v, int i) {
    map[u][v] = map[v][u] = true;
    p[i][0] = u, p[i][1] = v;
    eid[u] = i, eid[v] = i;
    deg[u]++, deg[v]++;
  }

  void dfs(int x, int f) {
    fa[x] = f;
    for (int i = heads[x]; i; i = e[i].nxt)
      if (e[i].v != f) dfs(e[i].v, x);
  }
} A, B;

int need[maxn];
int heads[maxn], ecnt;
int deg[maxn];

inline void add(int u, int v) {
  e[++ecnt].v = v, e[ecnt].nxt = heads[u], heads[u] = ecnt, deg[v]++;
}

inline bool topoSort() {
  static bool vis[maxn];
  static std::queue<int> q;
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; ++i)
    if (!deg[i]) vis[i] = true, q.push(i);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = heads[u]; i; i = e[i].nxt) {
      int v = e[i].v;
      deg[v]--;
      if (!deg[v]) vis[v] = true, q.push(v);
    }
  }
  for (int i = 1; i <= n; ++i)
    if (!vis[i]) return false;
  return true;
}

inline int solve(int rt, int rtFa, int id) {
  memset(A.heads, 0, sizeof(A.heads));
  A.ecnt = 0;
  for (int i = 1; i < n; ++i) {
    if (i == id) continue;
    A.add(A.p[i][0], A.p[i][1]);
    A.add(A.p[i][1], A.p[i][0]);
  }
  A.add(rt, rtFa);
  A.add(rtFa, rt);
  A.dfs(rt, 0);
  B.dfs(rt, 0);
  int res = 0;
  for (int i = 1; i <= n; ++i) need[i] = (A.fa[i] != B.fa[i]), res += need[i];
  memset(heads, 0, sizeof(heads));
  ecnt = 0;
  memset(deg, 0, sizeof(deg));
  for (int i = 1; i <= n; ++i) {
    if (i == rt) continue;
    if (!need[i] && need[A.fa[i]]) return inf;
    if (need[i] && need[A.fa[i]]) add(i, A.fa[i]);
    if (need[i] && need[B.fa[i]]) add(B.fa[i], i);
  }
  return topoSort() ? res : inf;
}

inline int main() {
  int T;
  cin >> T;
  while (T--) {
    cin >> n;
    A.clear(), B.clear();
    for (int i = 1; i < n; ++i) {
      int u, v;
      cin >> u >> v;
      A.link(u, v, i);
    }
    for (int i = 1; i < n; ++i) {
      int u, v;
      cin >> u >> v;
      B.add(u, v);
      B.add(v, u);
      B.link(u, v, i);
    }
    int ans = inf;
    for (int rt = 1; rt <= n; ++rt)
      if (A.deg[rt] == 1)
        for (int f = 1; f <= n; ++f)
          if (f != rt) ans = std::min(ans, (A.map[rt][f] ^ 1) + solve(rt, f, A.eid[rt]));
    cout << (ans == inf ? -1 : ans) << '\n';
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("AT4380 [AGC027F] Grafting.in", "r", stdin);
  freopen("AT4380 [AGC027F] Grafting.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
